package common.services;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import common.filter.QueryFilter;
import common.model.BaseModel;
import common.util.Get32Primarykey;

public abstract class BaseServiceImpl extends SqlMapClientDaoSupport {

	protected static final String PRIMARY_KEY = "ID";
	protected static final String FINDALL_PREFFIX = "findAll";
	protected static final String FINDALL_SUFFIX = "List";
	protected static final String INSERT_PREFFIX = "insert";
	protected static final String DELETE_PREFFIX = "delete";
	protected static final String UPDATE_PREFFIX = "update";
	protected static final String FIND_PREFFIX = "find";
	protected static final String BYID_SUFFIX = "ById";
	protected static final String BYFILTER_SUFFIX = "ListByFilter";
	protected final Log log = LogFactory.getLog(getClass());


	public BaseServiceImpl() {
	}

	protected  String getDomainName() {
		return null;
	}

	public void deleteEntityById(String id) throws Exception {
		getSqlMapClientTemplate().delete(getDeleteStatementId(), id);
	}

	public BaseModel findEntityById(String id) {
		return (BaseModel) getSqlMapClientTemplate().queryForObject(getFindbyStatementId(), id);
	}

	public List findEntityListByFilter(QueryFilter filter) {
		return getSqlMapClientTemplate().queryForList(getFindbyfilterStatementId(), filter);
	}

	public String insertEntity(BaseModel entity) throws Exception {
		entity.setId(Get32Primarykey.getRandom32BeginTimePK());
		if(entity.getCreateDatetime()==null){
			entity.setCreateDatetime(new Date());
		}
		getSqlMapClientTemplate().insert(getInsertStatementId(), entity);
		return entity.getId();
	}

	public void updateEntity(BaseModel entity) throws Exception {
		if(entity.getUpdateDatetime()==null){
			entity.setCreateDatetime(new Date());
		}
		getSqlMapClientTemplate().update(getUpdateStatementId(), entity);
	}

	public List findAllEntityList() {
		return getSqlMapClientTemplate().queryForList(getFindAllStatementId());
	}

	protected String getDeleteStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "delete" + getDomainName() + "ById";
	}

	protected String getInsertStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "insert" + getDomainName();
	}

	protected String getUpdateStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "update" + getDomainName() + "ById";
	}

	protected String getFindbyStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "find" + getDomainName() + "ById";
	}

	protected String getFindbyfilterStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "find" + getDomainName() + "ListByFilter";
	}

	protected String getFindAllStatementId() {
		if (StringUtils.isEmpty(getDomainName())) {
			log.error("Currently no domain name specified in your ServiceImpl class which inherited from BaseServiceImpl");
			log.error("You have to implement method getDomainName() in order to execute sql");
		}
		return "findAll" + getDomainName() + "List";
	}
}
